#!/bin/bash
#
# USAGE:
#	m_readfiles
#
# DESCRIPTION:
#  m_readfiles is a builtin ADORE process.  
#  It runs the DORIS step with the same name. 
#
# This is the first step if the ERS1/2 SLC images are processed.
# The SLC leader, volume and (header of the) data file are read, and relevant parameters are written to the master result file specified by the general card M_RESFILE. These parameters are used in the further processing. Currently, ERS1/2 SLC and ENVISAT SLC files can be read. If the output of this step is mimicked, Doris can be tricked to process the other steps. The sole purpose of this step is to create result file where relevant parameters are stored (PRF, wavelength, etc.), also see the example in the next section.
#
# In order to be compatible with official DORIS release v4.02, the current ADORE implementation makes calls for reading Gamma SLC files, if $slcType is specified as gamma. This will be removed when DORIS v4.03 is released, because it will support reading Gamma SLC files within DORIS.
#
# INPUT:
#
# OUTPUT:
#  Outputs generated by Doris:
#   Master result file
#
# REFERENCE:
# http://doris.tudelft.nl/usermanual/node13.html 


slcType=`echo ${slcType-undefined} | tr "[:upper:]" "[:lower:]"`
#echo ${slcType}
#check if slcType exists
case "${slcType}" in
  gamma)
    echo "Reading Gamma SLC"
    masterDataFolder=`dirname "${m_in_dat}"`
    #get parFile SysFile
    parFile=`ls ${dataFolder}/${master}/${gammaParFile} 2>/dev/null | tr -d '\n'`
    if [ ! -e "${parFile}" ]; then
      echo " I can not find the parameter file ("${gammaParFile}") inside ${masterDataFolder}"
      return -1;
    fi

    sysFile=`ls ${dataFolder}/${master}/${gammaSysFile} 2>/dev/null | tr -d '\n'`
    if [ ! -e "${sysFile}" ]; then
      echo " I can not find the parameter file ("${gammaSysFile}") inside ${masterDataFolder}"
      return -1;
    fi
    
    gammaReadfiles.csh ${parFile} ${sysFile} ${m_in_dat} ${gammaByteSwap} > ${outputFolder}/${master}.res
    [ $? -eq 0 ] && echo "${dorisProcess}: SUCCESS"
    unset masterDataFolder parFile sysFile
  ;;
  adore)
    echo "Reading ADORE SLC"
    if [ -e "${m_in_lea}" ];then
      #copy header
      headerLine=`grep -n Start_process_control ${m_in_lea} | cut -d":" -f1`
      headerLine=$(( ${headerLine} - 1 ))
      head -n ${headerLine} ${m_in_lea} > ${m_resfile}
      tailLine=`grep -n End_process_control ${m_in_lea} | cut -d":" -f1`
      grep -A $(( $tailLine - $headerLine )) Start_process_control  ${m_in_lea} | tr "1" "0" >> ${m_resfile}
      # copy readfiles 
      copyRes.sh ${m_in_lea} readfiles ${m_resfile}  
      modifyRes.sh ${m_resfile} readfiles "Datafile" ${m_in_dat}
    else
      error "ADORE SLC files also require a leader file set."
      return -1;
    fi
  ;;
  gmtsar)
    echo "Reading GMTSAR SLC"
    if [ -e "${m_in_lea}" ] && [ -e "${m_in_dat}" ] && [ -e "${m_in_vol}" ]; then
      #modify m_crop_in
      settings apply -r -q _rel_m_crop_in='echo ${m_in_dat}.ci2'
      #run the m_readfiles script
      gmtsarReadfiles.py ${m_in_dat} ${m_in_lea} ${m_in_vol} > ${m_resfile}
      [ $? -eq 0 ] && echo "${dorisProcess}: SUCCESS"
    else 
      error "GMTSAR SLC files require the *.PRM, *.SLC and *.LEA files in m_in_lea, m_in_dat and m_in_vol."
      return -1;
    fi
  ;;
  *)
    #if [ "${slcType-undefined}" == "undefined" ]; then
    #standard processing
    pp ${dorisProcess}
    doris ${outputFolder}/${dorisProcess}.drs
    [ $? -eq 0 ] && echo "${dorisProcess}: SUCCESS"
  ;;
esac
